package com.hdu.mockService.factory;

import com.hdu.mockService.handler.IMockHandler;
import com.hdu.mockService.utils.ExtensionServiceLoader;

import java.util.List;

public class MockHandlerFactory {
    private static final List<IMockHandler> HANDLERS;

    static {
        HANDLERS = ExtensionServiceLoader.get(IMockHandler.class);
    }


    @SuppressWarnings("unchecked")
    public static <T> T getHandlerByType(Class<T> clazz) {
        return (T) HANDLERS.stream().filter(handler -> handler.getClass().equals(clazz))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("class not fount , class : " + clazz.getSimpleName()));
    }

    public static List<IMockHandler> getHandlers() {
        return HANDLERS;
    }

}
